<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * organisasi.php
 * @author ranuandra
 *
 * 26 Mei 2012 14:59:11
 *
 * 
 */

class Controller_Organisasi extends Kohana_Nasmoco
{
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$datas = $pelamar->pglmnorganisasi->find_all();
		$view = View::factory('organisasi/index');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_add()
	{
		$view = View::factory('organisasi/form');
		$this->template->container = $view;
	}
	
	public function action_save()
	{

/*		print_r($_POST);
		if(isset($_POST['submit']))
		{*/
			$db = Database::instance();
			$db->begin();

			try 
			{
				$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
				if(isset($_POST['id']))
				{
					$organisasi = ORM::factory('pglmnorganisasi', Request::current()->post('id'));
					$organisasi->nama_organisasi = Request::current()->post('nama_organisasi');
					$organisasi->posisi = Request::current()->post('posisi');
					$organisasi->lama = Request::current()->post('lama');
					$organisasi->save();
				}
				else 
				{
					if(isset($_POST['flag_organisasi']))
					{
						$pelamar->flag_organisasi = Request::current()->post('flag_organisasi');
						$pelamar->save();
					}
					if(isset($_POST['nama_organisasi']))
					{
						$organisasi = ORM::factory('pglmnorganisasi');
						$organisasi->mstpelamar_id = $pelamar->id;
						$organisasi->create_data($_POST);
					}
				}

				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		//}		
	}
}